package alan.springboot2x.swagger.knife4j.controller;

import alan.springboot2x.swagger.knife4j.service.AsyncService;
import alan.springboot2x.swagger.knife4j.service.AsyncServiceV1Impl;
import alan.springboot2x.swagger.knife4j.service.AsyncServiceV2Impl;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author: Alan
 * @date: 2021/4/29 23:22
 */
@RestController
@RequestMapping("async")
@Slf4j
public class AsyncController {

    @Autowired
    AsyncService asyncService;

    @Autowired
    AsyncServiceV2Impl asyncServiceV2;

    @Autowired
    AsyncServiceV1Impl asyncServiceV1;



    @ApiOperation(value="测试接口", notes="测试接口")
    @PostMapping("test")
    public String test(String name){
        long  t1=System.currentTimeMillis();
        Future<String> n = asyncService.getName(name);
        Future<Integer> age =  asyncService.getAge();
        Map<String,Object> map =new HashMap<String,Object>();
        try {
            // 调用Feture<V>对象的get方法会产生阻塞，直到有结果返回
            map.put("name",  n.get());
            map.put("age",  age.get());
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        long  t2=System.currentTimeMillis();
        long  sec= (t2-t1)/1000;
        System.out.println("耗时:"+sec+"秒");
        //4秒
        map.put("ts",sec+"秒");
        return JSONUtil.toJsonStr(map);
    }

    @ApiOperation(value="测试接口", notes="测试接口")
    @PostMapping("test2")
    public String test2(String name){
        long  t1=System.currentTimeMillis();
        Map<String,Object> map =new HashMap<String,Object>();
        try {
            Future<String> n = asyncServiceV1.getNameTest(name);
            Future<Integer> age =  asyncServiceV1.getAge();
            // 调用Feture<V>对象的get方法会产生阻塞，直到有结果返回
            map.put("name",  n.get());
            map.put("age",  age.get());
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        long  t2=System.currentTimeMillis();
        long  sec= (t2-t1)/1000;
        System.out.println("耗时:"+sec+"秒");
        //4秒
        map.put("ts",sec+"秒");
        return JSONUtil.toJsonStr(map);
    }

    @ApiOperation(value="测试接口", notes="测试接口")
    @PostMapping("test3")
    public String test3(String name){
        long  t1=System.currentTimeMillis();
        Map<String,Object> map =new HashMap<String,Object>();
        try {
            Future<String> n = asyncServiceV2.getNameTest(name);
            Future<Integer> age =  asyncServiceV2.getAge();
            // 调用Feture<V>对象的get方法会产生阻塞，直到有结果返回
            map.put("name",  n.get());
            map.put("age",  age.get());
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }
        long  t2=System.currentTimeMillis();
        long  sec= (t2-t1)/1000;
        System.out.println("耗时:"+sec+"秒");
        //4秒
        map.put("ts",sec+"秒");
        return JSONUtil.toJsonStr(map);
    }

}
